<?php
namespace Admin\Controller;
use Common\Controller\BackendController;

vendor('taobao.TopClient');
vendor('taobao.RequestCheckUtil');
vendor('taobao.ResultSet');
vendor('taobao.request.TbkItemInfoGetRequest');
import("ITK.ORG.Page");

class ItemsController extends BackendController 
{
    public function _initialize()
    {
        parent::_initialize();
        $this->_mod = D('Items');
        $this->_cate_mod = D('ItemsCate');
    }
    public function pass()
    {
        if (IS_POST) {
            $data['pass'] = $this->_post('pass', 'intval');
            $ids = $this->_post('ids');
            $ids_arr = explode(',', $ids);
            $datas['pass'] = $data['pass'];
            $this->_mod->where(array('id' => array('in', $ids)))->save($datas);
            $this->ajaxReturn(1, L('operation_success'), '', 'pass');
        } else {
            $ids = trim(I('id'), ',');
            $this->assign('ids', $ids);
            $resp = $this->fetch();
            $this->ajaxReturn(1, '', $resp);
        }
    }
    public function key_atime()
    {
        if (IS_POST) {
            $action = $this->_post('action', 'trim');
            $today_str = $this->_post('start', 'trim');
            $today_str = strtotime($today_str);
            $tomorr_str = $this->_post('end', 'trim');
            $tomorr_str = strtotime($tomorr_str);
            $today_wh['add_time'] = array(array('egt', $today_str), array('elt', $tomorr_str));
            $times = $this->_post('times', 'trim');
            $endtimes = $this->_post('endtimes', 'trim');
            !$times && $this->error('请输入开抢时间', U('items/key_atime'));
            !$endtimes && $this->error('请输入结束时间', U('items/key_atime'));
            $datas['coupon_start_time'] = strtotime($times);
            $datas['coupon_end_time'] = strtotime($endtimes);
            if ($datas) {
                $this->_mod->where($today_wh)->save($datas);
                $this->success('设置时间成功！');
            } else {
                $this->error('错误', U('Items/key_atime'));
            }
        } else {
            $this->display();
        }
    }
    public function do_top()
    {
        if (IS_POST) {
            $data['top'] = $this->_post('do_top', 'intval');
            $ids = $this->_post('ids');
            $ids_arr = explode(',', $ids);
            $this->_mod->where(array('id' => array('in', $ids)))->save($data);
            $this->ajaxReturn(1, L('operation_success'), '', 'do_top');
        } else {
            $ids = trim(I('id'), ',');
            $this->assign('ids', $ids);
            $resp = $this->fetch();
            $this->ajaxReturn(1, '', $resp);
        }
    }
    public function addtm()
    {
        if (IS_POST) {
            $coupon_start_time = strtotime($_POST['coupon_start_time']);
            $coupon_end_time = strtotime($_POST['coupon_end_time']);
            $datas['coupon_start_time'] = $coupon_start_time;
            $datas['coupon_end_time'] = $coupon_end_time;
            $ids = $this->_post('ids');
            $ids_arr = explode(',', $ids);
            $this->_mod->where(array('id' => array('in', $ids)))->save($datas);
            $this->ajaxReturn(1, L('operation_success'), '', 'addtm');
        } else {
            $ids = trim(I('id'), ',');
            $this->assign('ids', $ids);
            $resp = $this->fetch();
            $this->ajaxReturn(1, '', $resp);
        }
    }
    public function move()
    {
        if (IS_POST) {
            $data['pid'] = $this->_post('cate_id', 'intval');
            $ids = $this->_post('ids');
            $ids_arr = explode(',', $ids);
            $datas['cate_id'] = $data['pid'];
            $datas['sex'] = $this->_post('sex', 'intval');
            $this->_mod->where(array('id' => array('in', $ids)))->save($datas);
            $this->ajaxReturn(1, L('operation_success'), '', 'move');
        } else {
            $ids = trim(I('id'), ',');
            $this->assign('ids', $ids);
            $resp = $this->fetch();
            $this->ajaxReturn(1, '', $resp);
        }
    }
    public function sex()
    {
        if (IS_POST) {
            $data['sex'] = $this->_post('sex', 'intval');
            $ids = $this->_post('ids');
            $ids_arr = explode(',', $ids);
            $datas['sex'] = $data['sex'];
            $this->_mod->where(array('id' => array('in', $ids)))->save($datas);
            $this->ajaxReturn(1, L('operation_success'), '', 'sex');
        } else {
            $ids = trim(I('id'), ',');
            $this->assign('ids', $ids);
            $resp = $this->fetch();
            $this->ajaxReturn(1, '', $resp);
        }
    }
    public function _before_index()
    {
        $res = $this->_cate_mod->field('id,name')->select();
        $cate_list = array();
        foreach ($res as $val) {
            $cate_list[$val['id']] = $val['name'];
        }
        $this->assign('cate_list', $cate_list);
        $this->sort = 'ordid ASC,';
        $this->order = 'add_time DESC';
    }
    public function ajax_upload_img()
    {
        $type = $this->_get('type', 'trim', 'img');
        if (!empty($_FILES[$type]['name'])) {
            $dir = date('ym/d/');
            $result = $this->_upload($_FILES[$type], 'item/' . $dir);
            if ($result['error']) {
                $this->ajaxReturn(0, $result['info']);
            } else {
                $savename = $dir . $result['info'][0]['savename'];
                $this->ajaxReturn(1, L('operation_success'), $savename);
            }
        } else {
            $this->ajaxReturn(0, L('illegal_parameters'));
        }
    }
    public function paypass()
    {
        if ($this->_request('sort', 'trim')) {
            $sort = $this->_request('sort', 'trim');
        } else {
            $sort = 'id';
        }
        if ($this->_request('order', 'trim')) {
            $order = $this->_request('order', 'trim');
        } else {
            $order = 'DESC';
        }
        $res = $this->_cate_mod->field('id,name')->select();
        $cate_list = array();
        foreach ($res as $val) {
            $cate_list[$val['id']] = $val['name'];
        }
        $this->assign('cate_list', $cate_list);
        $map['pass'] = 1;
        $map['pay'] = 1;
        $count = $this->_mod->where($map)->count('id');
        
        $pager = new \Page($count, 20);
        $select = $this->_mod->where($map)->order('id DESC');
        $select->order($sort . ' ' . $order);
        $select->limit($pager->firstRow . ',' . $pager->listRows);
        $page = $pager->show();
        $this->assign('page', $page);
        $list = $select->select();
        $this->assign('list', $list);
        $this->assign('list_table', true);
        $this->display();
    }
    public function payend()
    {
        if (I('sort', '', 'trim')) {
            $sort = I('sort', '', 'trim');
        } else {
            $sort = 'id';
        }
        if (I('order', '', 'trim')) {
            $order = I('order', '', 'trim');
        } else {
            $order = 'DESC';
        }
        $res = $this->_cate_mod->field('id,name')->select();
        $cate_list = array();
        foreach ($res as $val) {
            $cate_list[$val['id']] = $val['name'];
        }
        $this->assign('cate_list', $cate_list);
        $map['pass'] = 3;
        $map['pay'] = 1;
        $count = $this->_mod->where($map)->count('id');
        $pager = new \Page($count, 20);
        $select = $this->_mod->where($map)->order('id DESC');
        $select->order($sort . ' ' . $order);
        $select->limit($pager->firstRow . ',' . $pager->listRows);
        $page = $pager->show();
        $this->assign('page', $page);
        $list = $select->select();
        $this->assign('list', $list);
        $this->assign('list_table', true);
        $this->display();
    }
    public function payouttime()
    {
        if (IS_POST) {
            $action = $this->_post('action', 'trim');
            $where = '  pass=1  AND coupon_end_time <' . time();
            $this->_mod->where($where)->select();
        } else {
            if (I('sort', '', 'trim')) {
                $sort = I('sort', '', 'trim');
            } else {
                $sort = 'id';
            }
            if (I('order', '', 'trim')) {
                $order = I('order', '', 'trim');
            } else {
                $order = 'DESC';
            }
            $map['pass'] = 1;
            $map['pay'] = 1;
            $map['coupon_end_time'] = array('elt', time());
            $count = $this->_mod->where($map)->count('id');
            $pager = new \Page($count, 20);
            $select = $this->_mod->where($map)->order('id DESC');
            $select->order($sort . ' ' . $order);
            $select->limit($pager->firstRow . ',' . $pager->listRows);
            $page = $pager->show();
            $this->assign('page', $page);
            $list = $select->select();
            $this->assign('list', $list);
            $this->assign('list_table', true);
            $this->display();
        }
    }
    protected function _search()
    {
        $map = array();
        ($time_start = I('time_start', '', 'trim')) && ($map['add_time'][] = array('egt', strtotime($time_start)));
        ($time_end = I('time_end', '', 'trim')) && ($map['add_time'][] = array('elt', strtotime($time_end) + (24 * 60 * 60 - 1)));
        ($price_min = I('price_min', '', 'trim')) && ($map['coupon_price'][] = array('egt', $price_min));
        ($price_max = I('price_max', '', 'trim')) && ($map['coupon_price'][] = array('elt', $price_max));
        ($volume_min = I('volume_min', '', 'trim')) && ($map['volume'][] = array('egt', $volume_min));
        ($volume_max = I('volume_max', '', 'trim')) && ($map['volume'][] = array('elt', $volume_max));
        ($nick = I('nick', '', 'trim')) && ($map['nick'] = array('like', '%' . $nick . '%'));
        ($shop_type = I('shop_type', '', 'trim')) && ($map['shop_type'] = array('like', '%' . $shop_type . '%'));
        ($num_iid = I('num_iid', '', 'trim')) && ($map['num_iid'] = array('like', '%' . $num_iid . '%'));
        ($paytid = I('paytid', '', 'trim')) && ($map['paytid'] = array('like', '%' . $paytid . '%'));
        ($que = I('que', '', 'trim')) && ($map['que'] = array('like', '%' . $que . '%'));
        $cate_id = I('cate_id', '', 'intval');
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id' => $cate_id))->getField('spid');
            if ($spid == 0) {
                $spid = $cate_id;
            } else {
                $spid .= $cate_id;
            }
        }
        $map['pass'] = 1;
        ($keyword = I('keyword', '', 'trim')) && ($map['title'] = array('like', '%' . $keyword . '%'));
        $this->assign('search', array('time_start' => $time_start, 'time_end' => $time_end, 'price_min' => $price_min, 'price_max' => $price_max, 'volume_min' => $volume_min, 'volume_max' => $volume_max, 'nick' => $nick, 'paytid' => $paytid, 'que' => $que, 'shop_type' => $shop_type, 'num_iid' => $num_iid, 'pass' => $pass, 'selected_ids' => $spid, 'cate_id' => $cate_id, 'keyword' => $keyword));
        return $map;
    }
    private function __getItemsOrig()
    {
        $orig = M('ItemsOrig')->field('id,type')->select();
        $result = array();
        if (!is_array($orig)) {
            $orig = array();
        }
        foreach ($orig as $item) {
            $result[$item['type']] = $item['id'];
        }
        return $result;
    }
    public function add()
    {
        if (IS_POST) {
            if (FALSE === ($data = $this->_mod->create())) {
                $this->error($this->_mod->getError());
            }
            if (!trim($data['cate_id'])) {
                $this->error('请选择商品分类');
            }
            if (!trim($data['num_iid'])) {
                $this->error('商品ID为空');
            }
            if ($this->_mod->where(array('num_iid' => $data['num_iid']))->count()) {
                $this->error('商品已存在，请更换其他商品');
            }
            $data['sex'] = $_POST['sex'];
            $data['coupon_start_time'] = strtotime($_POST['coupon_start_time']);
            $data['coupon_end_time'] = strtotime($_POST['coupon_end_time']);
            $data['astime'] = date('Ymd');
            
            $item_id = $this->_mod->add($data);
            if (!$item_id) {
                $this->error(l('operation_failure'));
            }
            $this->success(l('operation_success'));
        } else {
            $orig_list = M('ItemsOrig')->where(array('pass' => 1))->select();
            $this->assign('orig_list', $orig_list);
            $this->display();
        }
    }
    public function ajaxGetItem()
    {
        if (!isset($_REQUEST['link'])) {
            $this->ajaxReturn(0, '未传入商品链接');
        }
        $geturl = parse_url($_REQUEST['link']);
        $paramid = convertUrlQuery($geturl['query']);
        if (!$numid) {
            $numid = str_replace('http://www.meilishuo.com/share/item/', '', $_REQUEST['link']);
        } else {
            $numid = $paramid['id'];
        }
        if ($this->_mod->where(array('num_iid' => $numid))->count()) {
            echo '<script>alert(\'商品已存在\');</script>';
        }
        $info = getInfo($_REQUEST['link']);
        $tt = (string) ($info['coupon_price'] / $info['price']);
        $tt = substr($tt, 0, 4);
        $info['coupon_rate'] = $tt * 10000;
        $this->ajaxReturn(1, '', $info);
    }
    public function ajax_getid()
    {
        $url = $this->_get('url', 'trim');
        if (strpos($url, 'meilishuo.com')) {
            $num_iid = str_replace('http://www.meilishuo.com/share/item/', '', $url);
        } else {
            $u = parse_url($url);
            $param = convertUrlQuery($u['query']);
            $num_iid = $param['id'];
        }
        if ($this->_mod->where(array('num_iid' => $num_iid))->count()) {
            $this->ajaxReturn(0, '该商品已存在');
        } else {
            $this->ajaxReturn(1, l('operation_success'), $num_iid);
        }
    }
    public function edit()
    {
        if (IS_POST) {
            if (false === ($data = $this->_mod->create())) {
                $this->error($this->_mod->getError());
            }
            if (!$data['cate_id'] || !trim($data['cate_id'])) {
                $this->error('请选择商品分类');
            }
            $item_id = $data['id'];
            $data['coupon_start_time'] = strtotime($data['coupon_start_time']);
            $data['coupon_end_time'] = strtotime($data['coupon_end_time']);
            $this->_mod->where(array('id' => $item_id))->save($data);
            $this->success(L('operation_success'));
        } else {
            $id = $this->_get('id', 'intval');
            $item = $this->_mod->where(array('id' => $id))->find();
            $spid = $this->_cate_mod->where(array('id' => $item['cate_id']))->getField('spid');
            if ($spid == 0) {
                $spid = $item['cate_id'];
            } else {
                $spid .= $item['cate_id'];
            }
            $this->assign('selected_ids', $spid);
            $item['desc'] = htmlspecialchars_decode($item['desc']);
            $this->assign('info', $item);
            $orig_list = M('ItemsOrig')->select();
            $this->assign('orig_list', $orig_list);
            $this->display();
        }
    }
    public function sellout()
    {
        if (IS_POST) {
            $action = $this->_post('action', 'trim');
            $where = '  pass=0  AND status =sellout';
            $this->_mod->where($where)->select();
        } else {
            if (I('sort', '', 'trim')) {
                $sort = I('sort', '', 'trim');
            } else {
                $sort = 'id';
            }
            if (I('order', '', 'trim')) {
                $order = I('order', '', 'trim');
            } else {
                $order = 'DESC';
            }
    //        $map['pass'] = 1;
            $map['status'] = 'sellout';
            $count = $this->_mod->where($map)->count('id');
            $pager = new \Page($count, 20);
            $select = $this->_mod->where($map)->order('id DESC');
            $select->order($sort . ' ' . $order);
            $select->limit($pager->firstRow . ',' . $pager->listRows);
            $page = $pager->show();
            $this->assign('page', $page);
            $list = $select->select();
            $this->assign('list', $list);
            $this->assign('list_table', true);
            $this->display('outtime');
        }
    }
    public function clear()
    {
        if (IS_POST) {
            if ($_REQUEST['isok'] == '1') {
                $action = $this->_post('action', 'trim');
                $where = '1=1';
                if ('outtime' == $action) {
                    $where .= ' AND pass=1  AND coupon_end_time <' . time();
                } elseif ('notpass' == $action) {
                    $where .= ' AND pass=0';
                } elseif ('clear' == $action) {
                    $db_file = CONF_PATH . 'db.php';
                    $db = (require $db_file);
                    $sql = 'TRUNCATE TABLE ' . $db['DB_PREFIX'] . 'items ';
                    $mes = M()->execute($sql);
                    $this->success(L('clear_success'));
                }elseif ('quanshixiao' == $action){
                	$where.= " AND status='sellout'";
                }
                $ids_list = $this->_mod->where($where)->select();
                $ids = '';
                foreach ($ids_list as $val) {
                    $ids .= $val['id'] . ',';
                }
                if ($ids != '') {
                    $ids = substr($ids, 0, -1);
                    M('items_comment')->where('item_id in(' . $ids . ')')->delete();
                }
                $this->_mod->where($where)->delete();
                $this->success(L('clear_success'));
            } else {
                $this->success('确认是否要删除？', U('items/clear'));
            }
        } else {
            $this->display();
        }
    }
    public function delete_search()
    {
        if (isset($_REQUEST['dosubmit'])) {
            if ($_REQUEST['isok'] == '1') {
                $where = '1=1';
                $keyword = trim($_POST['keyword']);
                $cate_id = trim($_POST['cate_id']);
                $cate_id = trim($_POST['cate_id']);
                $time_start = trim($_POST['time_start']);
                $time_end = trim($_POST['time_end']);
                $status = trim($_POST['status']);
                $min_price = trim($_POST['min_price']);
                $max_price = trim($_POST['max_price']);
                if ($keyword != '') {
                    $where .= ' AND title LIKE \'%' . $keyword . '%\'';
                }
                if ($cate_id != '' && $cate_id != 0) {
                    $where .= ' AND cate_id=' . $cate_id;
                }
                if ($time_start != '') {
                    $time_start_int = strtotime($time_start);
                    $where .= ' AND coupon_end_time>=\'' . $time_start_int . '\'';
                }
                if ($time_end != '') {
                    $time_end_int = strtotime($time_end);
                    $where .= ' AND coupon_end_time<=\'' . $time_end_int . '\'';
                }
                if ($status != '') {
                    $where .= ' AND status=' . $status;
                }
                if ($min_price != '') {
                    $where .= ' AND coupon_price>=' . $min_price;
                }
                if ($max_price != '') {
                    $where .= ' AND coupon_price<=' . $max_price;
                }
                $ids_list = $this->_mod->where($where)->select();
                $ids = '';
                foreach ($ids_list as $val) {
                    $ids .= $val['id'] . ',';
                }
                if ($ids != '') {
                    $ids = substr($ids, 0, -1);
                }
                $this->_mod->where($where)->delete();
                $this->success('删除成功', U('Items/clear'));
            } else {
                $this->success('确认是否要删除？', U('Items/clear'));
            }
        } else {
            $this->display();
        }
    }
    public function key_addtime()
    {
        if (IS_POST) {
            $action = $this->_post('action', 'trim');
            $where = '1=1';
            $where .= ' AND pass=1  AND coupon_end_time <' . time();
            if ('aday' == $action) {
                $datas['coupon_end_time'] = time() + 86400;
            } elseif ('twday' == $action) {
                $datas['coupon_end_time'] = time() + 2 * 86400;
            } elseif ('trday' == $action) {
                $datas['coupon_end_time'] = time() + 3 * 86400;
            } else {
                $times = $this->_post('times', 'intval');
                !$times && $this->error('请输入延时时间', U('items/key_addtime'));
                $datas['coupon_end_time'] = time() + $times * 3600;
            }
            if ($datas) {
                $this->_mod->where($where)->save($datas);
                $this->success('延时成功！');
            } else {
                $this->error('错误', U('items/key_addtime'));
            }
        } else {
            $this->display();
        }
    }
    public function check()
    {
        if (I('sort', '', 'trim')) {
            $sort = I('sort', '', 'trim');
        } else {
            $sort = 'id';
        }
        if (I('order', '', 'trim')) {
            $order = I('order', '', 'trim');
        } else {
            $order = 'DESC';
        }
        $res = $this->_cate_mod->field('id,name')->select();
        $cate_list = array();
        foreach ($res as $val) {
            $cate_list[$val['id']] = $val['name'];
        }
        $this->assign('cate_list', $cate_list);
        $map = array();
        $map['pass'] = 0;
        $map['status'] = 'underway';
        ($time_start = I('time_start', '', 'trim')) && ($map['add_time'][] = array('egt', strtotime($time_start)));
        ($time_end = I('time_end', '', 'trim')) && ($map['add_time'][] = array('elt', strtotime($time_end) + (24 * 60 * 60 - 1)));
        $cate_id = I('cate_id', '', 'intval');
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id' => $cate_id))->getField('spid');
            if ($spid == 0) {
                $spid = $cate_id;
            } else {
                $spid .= $cate_id;
            }
        }
        ($keyword = I('keyword', '', 'trim')) && ($map['title'] = array('like', '%' . $keyword . '%'));
        ($num_iid = I('num_iid', '', 'trim')) && ($map['num_iid'] = array('like', '%' . $num_iid . '%'));
        $this->assign('search', array('time_start' => $time_start, 'time_end' => $time_end, 'selected_ids' => $spid, 'cate_id' => $cate_id, 'keyword' => $keyword, 'num_iid' => $num_iid));
        $count = $this->_mod->where($map)->count('id');
        $pager = new \Page($count, 20);
        $select = $this->_mod->where($map)->order('id DESC');
        $select->limit($pager->firstRow . ',' . $pager->listRows);
        $select->order($sort . ' ' . $order);
        $page = $pager->show();
        $this->assign('page', $page);
        $listarr = $select->select();
        $lists = array();
        foreach ($listarr as $key => $val) {
            $lists[$key] = $val;
        }
        $this->assign('list', $lists);
        $this->assign('list_table', true);
        $this->display();
    }
    public function notcheck()
    {
        if (I('sort', '', 'trim')) {
            $sort = I('sort', '', 'trim');
        } else {
            $sort = 'id';
        }
        if (I('order', '', 'trim')) {
            $order = I('order', '', 'trim');
        } else {
            $order = 'DESC';
        }
        $res = $this->_cate_mod->field('id,name')->select();
        $cate_list = array();
        foreach ($res as $val) {
            $cate_list[$val['id']] = $val['name'];
        }
        $this->assign('cate_list', $cate_list);
        $map = array();
        $map['pass'] = 0;
        $map['status'] = 'fail';
        ($time_start = I('time_start', '', 'trim')) && ($map['add_time'][] = array('egt', strtotime($time_start)));
        ($time_end = I('time_end', '', 'trim')) && ($map['add_time'][] = array('elt', strtotime($time_end) + (24 * 60 * 60 - 1)));
        $cate_id = I('cate_id', '', 'intval');
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id' => $cate_id))->getField('spid');
            if ($spid == 0) {
                $spid = $cate_id;
            } else {
                $spid .= $cate_id;
            }
        }
        ($keyword = I('keyword', '', 'trim')) && ($map['title'] = array('like', '%' . $keyword . '%'));
        ($num_iid = I('num_iid', '', 'trim')) && ($map['num_iid'] = array('like', '%' . $num_iid . '%'));
        $this->assign('search', array('time_start' => $time_start, 'time_end' => $time_end, 'selected_ids' => $spid, 'cate_id' => $cate_id, 'keyword' => $keyword, 'num_iid' => $num_iid));
        $count = $this->_mod->where($map)->count('id');
        $pager = new \Page($count, 20);
        $select = $this->_mod->where($map)->order('id DESC');
        $select->limit($pager->firstRow . ',' . $pager->listRows);
        $select->order($sort . ' ' . $order);
        $page = $pager->show();
        $this->assign('page', $page);
        $listarr = $select->select();
        $lists = array();
        foreach ($listarr as $key => $val) {
            $lists[$key] = $val;
            $user = M('user')->where(array('username' => $val['uname']))->find();
        }
        $this->assign('list', $lists);
        $this->assign('list_table', true);
        $this->display();
    }
    public function fcheck()
    {
        if (I('sort', '', 'trim')) {
            $sort = I('sort', '', 'trim');
        } else {
            $sort = 'id';
        }
        if (I('order', '', 'trim')) {
            $order = I('order', '', 'trim');
        } else {
            $order = 'DESC';
        }
        $res = $this->_cate_mod->field('id,name')->select();
        $cate_list = array();
        foreach ($res as $val) {
            $cate_list[$val['id']] = $val['name'];
        }
        $this->assign('cate_list', $cate_list);
        $map = array();
        $map['pass'] = 2;
        $map['status'] = 'underway';
        ($time_start = I('time_start', '', 'trim')) && ($map['add_time'][] = array('egt', strtotime($time_start)));
        ($time_end = I('time_end', '', 'trim')) && ($map['add_time'][] = array('elt', strtotime($time_end) + (24 * 60 * 60 - 1)));
        $cate_id = I('cate_id', '', 'intval');
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id' => $cate_id))->getField('spid');
            if ($spid == 0) {
                $spid = $cate_id;
            } else {
                $spid .= $cate_id;
            }
        }
        ($keyword = I('keyword', '', 'trim')) && ($map['title'] = array('like', '%' . $keyword . '%'));
        ($num_iid = I('num_iid', '', 'trim')) && ($map['num_iid'] = array('like', '%' . $num_iid . '%'));
        $this->assign('search', array('time_start' => $time_start, 'time_end' => $time_end, 'selected_ids' => $spid, 'cate_id' => $cate_id, 'num_iid' => $num_iid, 'keyword' => $keyword));
        $count = $this->_mod->where($map)->count('id');
        $pager = new \Page($count, 20);
        $select = $this->_mod->where($map)->order('id DESC');
        $select->limit($pager->firstRow . ',' . $pager->listRows);
        $select->order($sort . ' ' . $order);
        $page = $pager->show();
        $this->assign('page', $page);
        $listarr = $select->select();
        $lists = array();
        foreach ($listarr as $key => $val) {
            $lists[$key] = $val;
            $user = M('user')->where(array('username' => $val['uname']))->find();
        }
        $this->assign('list', $lists);
        $this->assign('list_table', true);
        $this->display();
    }
    public function top()
    {
        if (I('sort', '', 'trim')) {
            $sort = I('sort', '', 'trim');
        } else {
            $sort = 'id';
        }
        if (I('order', '', 'trim')) {
            $order = I('order', '', 'trim');
        } else {
            $order = 'DESC';
        }
        $res = $this->_cate_mod->field('id,name')->select();
        $cate_list = array();
        foreach ($res as $val) {
            $cate_list[$val['id']] = $val['name'];
        }
        $this->assign('cate_list', $cate_list);
        $map = array();
        $map['pass'] = 1;
        $map['top'] = 1;
        $map['status'] = 'underway';
        ($time_start = I('time_start', '', 'trim')) && ($map['add_time'][] = array('egt', strtotime($time_start)));
        ($time_end = I('time_end', '', 'trim')) && ($map['add_time'][] = array('elt', strtotime($time_end) + (24 * 60 * 60 - 1)));
        $cate_id = I('cate_id', '', 'intval');
        if ($cate_id) {
            $id_arr = $this->_cate_mod->get_child_ids($cate_id, true);
            $map['cate_id'] = array('IN', $id_arr);
            $spid = $this->_cate_mod->where(array('id' => $cate_id))->getField('spid');
            if ($spid == 0) {
                $spid = $cate_id;
            } else {
                $spid .= $cate_id;
            }
        }
        ($keyword = I('keyword', '', 'trim')) && ($map['title'] = array('like', '%' . $keyword . '%'));
        ($num_iid = I('num_iid', '', 'trim')) && ($map['num_iid'] = array('like', '%' . $num_iid . '%'));
        $this->assign('search', array('time_start' => $time_start, 'time_end' => $time_end, 'selected_ids' => $spid, 'cate_id' => $cate_id, 'keyword' => $keyword, 'num_iid' => $num_iid));
        $count = $this->_mod->where($map)->count('id');
        $pager = new \Page($count, 20);
        $select = $this->_mod->where($map)->order('id DESC');
        $select->limit($pager->firstRow . ',' . $pager->listRows);
        $select->order($sort . ' ' . $order);
        $page = $pager->show();
        $this->assign('page', $page);
        $listarr = $select->select();
        $lists = array();
        foreach ($listarr as $key => $val) {
            $lists[$key] = $val;
        }
        $this->assign('list', $lists);
        $this->assign('list_table', true);
        $this->display();
    }
    public function f_check()
    {
        $mod = D($this->_name);
        $pk = $mod->getPk();
        $ids = trim(I($pk), ',');
        $datas['id'] = array('in', $ids);
        $datas['pass'] = 2;
        if ($datas) {
            if (false !== $mod->save($datas)) {
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'));
            } else {
                IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
            }
        } else {
            IS_AJAX && $this->ajaxReturn(0, L('illegal_parameters'));
        }
    }
    public function outtime()
    {
        if (IS_POST) {
            $action = $this->_post('action', 'trim');
            $where = '  pass=1  AND coupon_end_time <' . time();
            $this->_mod->where($where)->select();
        } else {
            if (I('sort', '', 'trim')) {
                $sort = I('sort', '', 'trim');
            } else {
                $sort = 'id';
            }
            if (I('order', '', 'trim')) {
                $order = I('order', '', 'trim');
            } else {
                $order = 'DESC';
            }
            $map['pass'] = 1;
            $map['coupon_end_time'] = array('elt', time());
            $count = $this->_mod->where($map)->count('id');
            $pager = new \Page($count, 20);
            $select = $this->_mod->where($map)->order('id DESC');
            $select->order($sort . ' ' . $order);
            $select->limit($pager->firstRow . ',' . $pager->listRows);
            $page = $pager->show();
            $this->assign('page', $page);
            $list = $select->select();
            $this->assign('list', $list);
            $this->assign('list_table', true);
            $this->display();
        }
    }
    public function fail()
    {
        if (I('sort', '', 'trim')) {
            $sort = I('sort', '', 'trim');
        } else {
            $sort = 'id';
        }
        if (I('order', '', 'trim')) {
            $order = I('order', '', 'trim');
        } else {
            $order = 'DESC';
        }
        $res = $this->_cate_mod->field('id,name')->select();
        $cate_list = array();
        foreach ($res as $val) {
            $cate_list[$val['id']] = $val['name'];
        }
        $this->assign('cate_list', $cate_list);
        $map['pass'] = 0;
        $map['status'] = 'fail';
        $count = $this->_mod->where($map)->count('id');
        $pager = new \Page($count, 20);
        $select = $this->_mod->where($map)->order('id DESC');
        $select->order($sort . ' ' . $order);
        $select->limit($pager->firstRow . ',' . $pager->listRows);
        $page = $pager->show();
        $this->assign('page', $page);
        $list = $select->select();
        $this->assign('list', $list);
        $this->assign('list_table', true);
        $this->display();
    }
    public function add_time()
    {
        $mod = D($this->_name);
        $pk = $mod->getPk();
        $ids = trim(I($pk), ',');
        $datas['id'] = array('in', $ids);
        $datas['coupon_end_time'] = time() + C('itk_item_add_time') * 3600;
        if ($datas) {
            if (false !== $mod->save($datas)) {
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'));
            } else {
                IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
            }
        } else {
            IS_AJAX && $this->ajaxReturn(0, L('illegal_parameters'));
        }
    }
    public function do_check()
    {
        $mod = D($this->_name);
        $pk = $mod->getPk();
        $ids = trim(I($pk), ',');
        $datas['id'] = array('in', $ids);
        $datas['pass'] = 1;
        if ($datas) {
            if (false !== $mod->save($datas)) {
                IS_AJAX && $this->ajaxReturn(1, L('operation_success'));
            } else {
                IS_AJAX && $this->ajaxReturn(0, L('operation_failure'));
            }
        } else {
            IS_AJAX && $this->ajaxReturn(0, L('illegal_parameters'));
        }
    }
    public function getcheck()
    {
        if (IS_POST) {
            $id = $this->_post('id', 'trim');
            $title = $this->_post('title', 'trim');
            $coupon_start_time = $this->_post('coupon_start_time', 'trim');
            $coupon_end_time = $this->_post('coupon_end_time', 'trim');
            $pass = $this->_post('pass', 'trim');
            $click_url = $this->_post('click_url', 'trim');
            $fail_reason = $this->_post('fail_reason', 'trim');
            $fpass_reason = $this->_post('fpass_reason', 'trim');
            $coupon_start_time = strtotime($coupon_start_time);
            $coupon_end_time = strtotime($coupon_end_time);
            $id = $this->_post('id', 'trim');
            if ($pass == 0) {
                $check_date['pass'] = 0;
                $check_date['fail_reason'] = $fail_reason;
                $check_date['status'] = 'fail';
                $this->_mod->where(array('id' => $id))->save($check_date);
                $this->ajaxReturn(1, L('operation_success'));
            } else {
                if ($pass == 2) {
                    $check_date['pass'] = 2;
                    $check_date['fail_reason'] = $fpass_reason;
                    $check_date['status'] = 'underway';
                    $this->_mod->where(array('id' => $id))->save($check_date);
                    $this->ajaxReturn(1, L('operation_success'));
                } else {
                    if ($pass == 1) {
                        $check_date['pass'] = 1;
                        $check_date['status'] = 'underway';
                        $check_date['title'] = $title;
                        $check_date['coupon_start_time'] = $coupon_start_time;
                        $check_date['coupon_end_time'] = $coupon_end_time;
                        $check_date['click_url'] = $click_url;
                        $this->_mod->where(array('id' => $id))->save($check_date);
                        $this->ajaxReturn(1, L('operation_success'));
                    }
                }
            }
        } else {
            $id = $this->_get('id', 'trim');
            !$id && $this->ajaxReturn(0, 'ID错误，请刷新');
            $item = $this->_mod->where(array('id' => $id))->find();
            $this->assign('item', $item);
            $resp = $this->fetch();
            $this->ajaxReturn(1, '审核管理', $resp);
        }
    }
    public function paiqi()
    {
        if (IS_POST) {
            $id = $this->_post('id', 'trim');
            $coupon_start_time = $this->_post('coupon_start_time', 'trim');
            $coupon_end_time = $this->_post('coupon_end_time', 'trim');
            $coupon_start_time = strtotime($coupon_start_time);
            $coupon_end_time = strtotime($coupon_end_time);
            $check_date['pass'] = 1;
            $check_date['status'] = 'underway';
            $check_date['coupon_start_time'] = $coupon_start_time;
            $check_date['coupon_end_time'] = $coupon_end_time;
            $this->_mod->where(array('id' => $id))->save($check_date);
            $this->ajaxReturn(1, L('operation_success'));
        } else {
            $id = $this->_get('id', 'trim');
            !$id && $this->ajaxReturn(0, 'ID错误，请刷新');
            $item = $this->_mod->where(array('id' => $id))->find();
            $payid = $item['payid'];
            $pay = D('Paydays')->where(array('id' => $payid))->find();
            $this->assign('item', $item);
            $this->assign('pay', $pay);
            $resp = $this->fetch();
            $this->ajaxReturn(1, '按排周期', $resp);
        }
    }
    public function ajax_gettags()
    {
        $title = $this->_get('title', 'trim');
        $tag_list = d('items')->get_tags_by_title($title);
        $tags = implode(',', $tag_list);
        $this->ajaxReturn(1, l('operation_success'), $tags);
    }
    public function ajax_getclick_url()
    {
        $iid = $this->_get('iid', 'trim');
        $url = 'http://item.taobao.com/item.htm?id=' . $iid;
        import("ITK.Itklib.itemcollect");
        $itemcollect = new \itemcollect();
        $itemcollect->url_parse($url);
        $item = $itemcollect->fetch_tb();
        \Think\Log::record('测试日志信息'.$item['click_url']);
        if ($item /*= $itemcollect->fetch_tb()*/) {
            $this->ajaxReturn(1, L('operation_success'), $item['click_url']);
        }
        $this->ajaxReturn(0, L('operation_failure'));
    }
}
function getInfo($url)
{
    $item_site = M('ItemsSite')->where(array('code' => 'meilishuo'))->find();
    $api_config = unserialize($item_site['config']);
    $na_id = $api_config['app_key'];
    $appkey = $api_config['app_secret'];
    if (strpos($url, 'meilishuo.com')) {
        $miid = str_replace('http://www.meilishuo.com/share/item/', '', $url);
        $murl = 'http://adsapi.meilishuo.com/goods/get_items_info?na_id=' . $na_id . '&appkey=' . $appkey . '&item_id=' . $miid . '&keyword=&goodstype=1&feedback=juanpi';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $murl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
        $file_contents = curl_exec($ch);
        curl_close($ch);
        if (!$file_contents) {
            $file_contents = file_get_contents($murl);
        }
        $data = getMeiLiShuoInfo($file_contents);
        $data['num_iid'] = $miid;
        return $data;
    } else {
        $u = parse_url($url);
        $param = convertUrlQuery($u['query']);
        $itemurl = 'http://item.taobao.com/item.htm?id=' . $param['id'];
        $itemurl = urlencode($itemurl);
        $data = getitemInfo($itemurl);
        $data['num_iid'] = $param['id'];
        $api_config = M('ItemsSite')->where(array('code' => 'taobao'))->getField('config');
        $config = unserialize($api_config);
        $c = new \TopClient();
        $c->appkey = $config['app_key'];
        $c->secretKey = $config['app_secret'];
        $req = new \TbkItemInfoGetRequest();
        $req->setFields('small_images');
        $req->setPlatform('1');
        $req->setNumIids($param['id']);
        $resp = $c->execute($req);
        $otheritem = objtoarr($resp);
        $citem = $otheritem['results']['n_tbk_item'][0]['small_images'];
        foreach ($citem[string] as $val) {
            $img .= '<img src="' . $val . '">';
        }
        $data['desc'] = $img;
        return $data;
    }
}
function getTaobaoShopInfo($content)
{
    $data = json_decode($content, true);
    $info = array();
    $tmp = json_decode($data['data']['apiStack'][0]['value'], true);
    $info['title'] = $data['data']['itemInfoModel']['title'];
    $tag_list = D('items')->get_tags_by_title($info['title']);
    $tags = implode(',', $tag_list);
    $info['tags'] = $tags;
    $info['volume'] = $tmp['data']['itemInfoModel']['totalSoldQuantity'];
    $info['ems'] = $tmp['data']['delivery']['deliveryFees'][0];
    $info['coupon_price'] = $tmp['data']['itemInfoModel']['priceUnits'][0]['price'];
    if (substr_count($info['coupon_price'], '-')) {
        $tmp1 = explode('-', $info['coupon_price']);
        $info['coupon_price'] = min($tmp1[0], $tmp1[1]);
    }
    $info['price'] = $tmp['data']['itemInfoModel']['priceUnits'][1]['price'];
    if (substr_count($info['price'], '-')) {
        $tmp = explode('-', $info['price']);
        $info['price'] = min($tmp[0], $tmp[1]);
    }
    $info['pic_url'] = $data['data']['itemInfoModel']['picsPath'][0];
    $info['pic_url'] = str_replace('_320x320.jpg', '', $info['pic_url']);
    $info['sellerId'] = $data['data']['seller']['userNumId'];
    $info['nick'] = $data['data']['seller']['nick'];
    $info['type'] = $data['data']['seller']['type'];
    $info['cu'] = $tmp['data']['itemInfoModel']['priceUnits'][0]['name'];
    if (!$info['cu']) {
        $info['cu'] = $tmp['data']['itemInfoModel']['priceUnits'][0]['tips'][0]['txt'];
    }
    $info['feedback'] = $data['data']['rateInfo']['rateDetailList'][0]['feedback'];
    $info['nicker'] = $data['data']['rateInfo']['rateDetailList'][0]['nick'];
    $info['headPic'] = $data['data']['rateInfo']['rateDetailList'][0]['headPic'];
    $info['feedback1'] = $data['data']['rateInfo']['rateDetailList'][1]['feedback'];
    $info['nicker1'] = $data['data']['rateInfo']['rateDetailList'][1]['nick'];
    $info['headPic1'] = $data['data']['rateInfo']['rateDetailList'][1]['headPic'];
    $info['feedback2'] = $data['data']['rateInfo']['rateDetailList'][2]['feedback'];
    $info['nicker2'] = $data['data']['rateInfo']['rateDetailList'][2]['nick'];
    $info['headPic2'] = $data['data']['rateInfo']['rateDetailList'][2]['headPic'];
    $info['feedback3'] = $data['data']['rateInfo']['rateDetailList'][3]['feedback'];
    $info['nicker3'] = $data['data']['rateInfo']['rateDetailList'][3]['nick'];
    $info['headPic3'] = $data['data']['rateInfo']['rateDetailList'][3]['headPic'];
    $info['feedback4'] = $data['data']['rateInfo']['rateDetailList'][4]['feedback'];
    $info['nicker4'] = $data['data']['rateInfo']['rateDetailList'][4]['nick'];
    $info['headPic4'] = $data['data']['rateInfo']['rateDetailList'][4]['headPic'];
    $info['descInfo'] = $data['data']['descInfo']['briefDescUrl'];
    $itaoke_https = new \itaoke_https();
    $itaoke_https->fetch($info['descInfo']);
    $source = $itaoke_https->results;
    if (!$source) {
        $source = file_get_contents($info['descInfo']);
    }
    $comlist = json_decode($source, true);
    $tm = $comlist['data']['images'];
    $onepic = '<img class="lazy" src=' . $tm[0] . '>';
    $zcitem['desc'] = implode('', $tm);
    foreach ($tm as $sms) {
        if (strpos($zcitem['desc'], $sms)) {
            $imgurl = '<img class="lazy" src=' . $sms . '>';
            $zcitem['desc'] = str_replace($sms, $imgurl, $zcitem['desc']);
            $zcitem['desc'] = str_replace($tm[0], '', $zcitem['desc']);
        }
    }
    $info['desc'] = $onepic . '' . $zcitem['desc'];
    return $info;
}
function getMeiLiShuoInfo($content)
{
    $data = json_decode($content, true);
    $info = array();
    $info['title'] = $data['data'][0]['title'];
    $tag_list = D('items')->get_tags_by_title($info['title']);
    $tags = implode(',', $tag_list);
    $info['tags'] = $tags;
    $info['click_url'] = $data['data'][0]['url'];
    $info['item_url'] = $data['data'][0]['wap_url'];
    $info['pic_url'] = $data['data'][0]['square_url'];
    $info['pic_urls'] = $data['data'][0]['pic_url'];
    $info['price'] = $data['data'][0]['oriprice'];
    $info['coupon_price'] = $data['data'][0]['discountprice'];
    $info['volume'] = $data['data'][0]['volume'] + 1;
    $info['inventory'] = $data['data'][0]['repertory'];
    $info['like'] = $data['data'][0]['like'];
    $info['intro'] = $data['data'][0]['recommend_comments'];
    $info['ems'] = '卖家包邮';
    $info['cu'] = '来自美丽说';
    $info['sellerId'] = $data['data'][0]['shop_id'];
    $info['nick'] = $data['data'][0]['shop_name'];
    $info['commission_rate'] = $data['data'][0]['commission_rate'];
    $commission_rate = $info['commission_rate'] * 1;
    $com_rate = round($commission_rate / 100 * 1, 2);
    $info['commission'] = $com_rate * $info['coupon_price'];
    $tm = $data['data'][0]['pic_url_detail'];
    $onepic = '<img class="lazy" src=' . $tm[0] . '>';
    $zcitem['desc'] = implode(',', $tm);
    foreach ($tm as $sms) {
        if (strpos($zcitem['desc'], $sms)) {
            $imgurl = '<img class="lazy" src=' . $sms . '>';
            $zcitem['desc'] = str_replace($sms, $imgurl, $zcitem['desc']);
            $zcitem['desc'] = str_replace($tm[0], '', $zcitem['desc']);
        }
    }
    $info['desc'] = $onepic . '' . $zcitem['desc'];
    $info['type'] = 'M';
    return $info;
}
function getitemInfo($url)
{
    $sourl = 'http://pub.alimama.com/items/search.json?q=' . $url . '&_t=1453007295246&auctionTag=&perPageSize=50&shopTag=&pvid=10_10.181.136.131_407_1453007270445&_tb_token_=';
    import("ITK.Itklib.itaoke_https");
    $itaoke_https = new \itaoke_https();
    $itaoke_https->fetch($sourl);
    $content = $itaoke_https->results;
    if (!$content) {
        $content = file_get_contents($sourl);
    }
    $dapi = json_decode($content, true);
    $data = array();
    $data['title'] = $dapi['data']['pageList'][0]['title'];
    $tag_list = D('items')->get_tags_by_title($data['title']);
    $tags = implode(',', $tag_list);
    $data['tags'] = $tags;
    $data['num_iid'] = $dapi['data']['pageList'][0]['auctionId'];
    $data['pic_url'] = $dapi['data']['pageList'][0]['pictUrl'];
    $data['price'] = $dapi['data']['pageList'][0]['reservePrice'];
    $data['coupon_price'] = $dapi['data']['pageList'][0]['zkPrice'];
    $data['nick'] = $dapi['data']['pageList'][0]['nick'];
    $data['sellerId'] = $dapi['data']['pageList'][0]['sellerId'];
    $data['commission'] = $dapi['data']['pageList'][0]['tkCommFee'];
    $data['commission_rate'] = $dapi['data']['pageList'][0]['tkRate'] * 100;
    $data['volume'] = $dapi['data']['pageList'][0]['biz30day'];
    $type = $dapi['data']['pageList'][0]['userType'];
    if ($type == 1) {
        $data['type'] = 'B';
    } else {
        $data['type'] = 'C';
    }
    return $data;
}
function convertUrlQuery($query)
{
    $queryParts = explode('&', $query);
    $params = array();
    foreach ($queryParts as $param) {
        $item = explode('=', $param);
        $params[$item[0]] = $item[1];
    }
    return $params;
}